package vy.org.app.page.lookup.patrika;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.vy.dal.dao.ViewVyLookupPatrikaPeriodDO;
import org.vy.dal.dao.lookup.VyLookupPatrikaDO;

public class PatrikaHelper {

	public static SimpleDateFormat _DATE_FMT = new SimpleDateFormat(
			"MM/dd/yyyy");

	public static MntnPatrikaForm fillPatrikaForm(VyLookupPatrikaDO dto) {
		MntnPatrikaForm form = new MntnPatrikaForm();
		form.setEffectiveDate(_DATE_FMT.format(dto.getEffectiveDate()));
		form.setLastUpdatedBy(dto.getLastUpdatedBy());
		form.setLastUpdatedOn(_DATE_FMT.format(dto.getLastUpdatedOn()));
		form.setPatrikaCode(dto.getPatrikaId().toString());
		form.setPatrikaName(dto.getPatrikaName());
		// form.setPerCopyAmount(dto.getPatrikaCost().toString());
		form.setWeekOffset(String.valueOf(dto.getWeekOffsetIndex()));

		return form;
	}

	public static VyLookupPatrikaDO fillPatrikaForm(MntnPatrikaForm form)
			throws Exception {
		VyLookupPatrikaDO dto = new VyLookupPatrikaDO();

		dto.setEffectiveDate(_DATE_FMT.parse(form.getEffectiveDate()));
		dto.setLastUpdatedBy(form.getLastUpdatedBy());
		dto.setLastUpdatedOn(new Date());
		if (form.getPatrikaCode() != null && !"".equals(form.getPatrikaCode()))
			dto.setPatrikaId(Integer.parseInt(form.getPatrikaCode()));
		dto.setPatrikaName(form.getPatrikaName());
		// if(form.getPerCopyAmount() != null &&
		// !"".equals(form.getPerCopyAmount()))
		// dto.setPatrikaCost(Double.parseDouble(form.getPerCopyAmount()));
		dto.setWeekOffsetIndex(Double.parseDouble(form.getWeekOffset()));

		return dto;
	}

	public static List<MntnPatrikaPeriodForm> fillPatrikaPeriodForm(
			VyLookupPatrikaDO dto,
			List<ViewVyLookupPatrikaPeriodDO> patPeriodLst) {
		List<MntnPatrikaPeriodForm> mntnPPFormLst = new ArrayList<MntnPatrikaPeriodForm>();
		if (patPeriodLst == null || patPeriodLst.isEmpty())
			return mntnPPFormLst;

		for (ViewVyLookupPatrikaPeriodDO patDO : patPeriodLst) {
			if (!dto.getPatrikaId().equals(patDO.getPatrikaId()))
				continue;
			MntnPatrikaPeriodForm form = new MntnPatrikaPeriodForm();
			form.setCost(patDO.getCost());

			form.setCountryCode(patDO.getCountryCode());
			form.setPatrikaName(patDO.getPatrikaName());
			form.setEffectiveDate(_DATE_FMT.format(patDO.getEffectiveDate()));
			form.setIsActive(patDO.getIsActive());
			form.setIsDeleted(patDO.getIsDeleted());
			form.setPatrikaId(patDO.getPatrikaId());
			form.setPatrikaName(patDO.getPatrikaName());
			form.setPatrikaPeriodId(patDO.getPatrikaPeriodId());

			form.setPeriodMonth(patDO.getPeriodMonth());
			form.setCurrency(patDO.getCurrency());
			form.setPeriodName(patDO.getPeriodName());
			form.setWeekOffsetIndex(patDO.getWeekOffsetIndex());

			if (patDO.getPeriodLastUpdatedBy() != null) {
				form.setPeriodLastUpdatedBy(patDO.getPeriodLastUpdatedBy());
			}

			if (patDO.getPeriodLastUpdatedOn() != null) {
				form.setPeriodLastUpdatedOn(_DATE_FMT.format(patDO
						.getPeriodLastUpdatedOn()));
			}

			if (patDO.getPatrikaLastUpdatedBy() != null) {
				form.setPatrikaLastUpdatedBy(patDO.getPatrikaLastUpdatedBy());
			}

			if (patDO.getPatrikaLastUpdatedOn() != null) {
				form.setPatrikaLastUpdatedOn(_DATE_FMT.format(patDO
						.getPatrikaLastUpdatedOn()));
			}

			mntnPPFormLst.add(form);
		}
		return mntnPPFormLst;
	}

}
